<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
      }

      body {
        height: 2000px;
        display: flex;
        flex-direction: column;
      }
      .top {
        height: 200px;
        text-align: center;
        overflow: hidden;
      }
      button {
        padding: 10px 20px;
        font-size: 30px;
        cursor: pointer;
      }
      .content {
        flex: 1;
        background-color: orange;
        display: flex;
      }
      .content .left {
        flex: 1;
        background-color: skyblue;
        padding: 10px;
        box-sizing: border-box;
      }
      .content .item {
        display: inline-block;
        height: 30px;
        border: 1px solid #000;
        padding: 4px;
        line-height: 30px;
        border-radius: 10px;
        transition: 0.3s;
        cursor: pointer;
        margin-right: 10px;
      }
      .content .item:hover {
        box-shadow: 0 0 10px gray;
        transform: translateY(-4px);
        color: white;
        font-weight: 900;
      }

      .content .right {
        flex: 1;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div class="top">
      <h2>抽奖</h2>
      <button>开始</button>
      <!-- toast -->
      <div
        class="position-fixed top-0 start-50 pt-4"
        style="z-index: 999; transform: translateX(30%)"
      >
        <div id="myToast" class="toast bg-rgba">
          <div class="toast-body"></div>
        </div>
      </div>
    </div>
    <div class="content">
      <div class="left">
        <div class="item">某同学-0次</div>
      </div>
      <div class="right"></div>
    </div>

    <script src="./bootstrap/bootstrap.min.js"></script>
    <script src="./lib/echarts.min.js"></script>
    <script src="../../day02/client/js/node_modules/axios/dist/axios.js"></script>
    <script>
      const left = document.querySelector(".left");
      let htmlArr = [];
      window.addEventListener("DOMContentLoaded", async () => {
        try {
          const studentData = await axios.get(
            "http://localhost:9728/api/students"
          );
          console.log(studentData.data);
          htmlArr = studentData.data;
          // 渲染左边数据
          render(htmlArr);
          renderChart(htmlArr);
          return htmlArr;
        } catch (err) {
          console.log(err);
        }
      });

      // 点击抽奖，抽取幸运学生，改变学生的count
      const btn = document.querySelector("button");
      btn.addEventListener("click", async () => {
        const luckyStu = await axios.get("http://localhost:9728/api/lucystar");
        console.log(luckyStu);
        tip(`恭喜*&nbsp;${luckyStu.data.name}&nbsp;*同学中奖了`);
        const stuData = await axios.get("http://localhost:9728/api/students");
        render(stuData.data);
        renderChart(stuData.data);
      });
      // 设置提示框
      const toastBox = document.querySelector("#myToast");
      // 生成一个Toast实例
      const toastBaby = new bootstrap.Toast(toastBox);
      // tip提示栏
      const tip = (msg) => {
        document.querySelector("#myToast").innerHTML = msg;
        toastBaby.show();
      };
      // 渲染数据
      function render(arr) {
        // console.log(arr);
        const resArr = arr.map((el) => {
          const { id, name, count } = el;
          return `
          <div class="item" data-id=${id}>${name}-${count}次</div>
          `;
        });
        left.innerHTML = resArr.join("");
      }
      // 生成图形
      function renderChart(arr) {
        var myChart = echarts.init(document.querySelector(".right"));
        const option = {
          xAxis: {
            type: "value",
          },
          yAxis: {
            type: "category",
            data: arr.map((el) => el.name),
            inverse: true,
          },
          tooltip: {},
          series: [
            {
              realtimeSort: true,
              data: arr.map((el) => el.count),
              type: "bar",
              showBackground: true,
              backgroundStyle: {
                color: "rgba(180, 180, 180, 0.2)",
              },
            },
          ],
        };
        myChart.setOption(option);
      }
    </script>
  </body>
</html>
